package edu.ahpu.propertymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ahpu.propertymanage.dataclass.PageSet;
import edu.ahpu.propertymanage.pojo.PropertyTage;
import edu.ahpu.propertymanage.pojo.Worker;
import edu.ahpu.propertymanage.service.PropertyTageService;
import edu.ahpu.propertymanage.service.WorkerService;
import edu.ahpu.propertymanage.util.GetID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/worker")
public class WorkerController {

    @Autowired
    WorkerService workerService;

    @Autowired
    GetID getID;

    @Autowired
    PropertyTageService propertyTageService;


    @GetMapping("/addworker")
    public String addWorker() {
        return getID.createWorkerUUID();
    }

    @PostMapping("/a&dworker")
    public String addorupdataWorker(@RequestBody Worker worker) {
        boolean bool = workerService.saveOrUpdate(worker);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("workerpage")
    public IPage<Worker> workerPage(@RequestBody PageSet pageSet) {
        IPage<Worker> workerIPage = new Page<>(pageSet.getCurrent(), pageSet.getSize());
        workerIPage = workerService.page(workerIPage);
        return workerIPage;
    }

    @PostMapping("deleteworker")
    public String deleteWorker(String workerId) {
        boolean bool = workerService.removeById(workerId);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("detailworker")
    public Worker detailWorker(String workerId){
        Worker worker = workerService.getById(workerId);
        return worker;
    }

    @PostMapping("searchworker")
    public List<Worker> searchWorker(String keyword){
        //正则表达式验证是否为手机号
        String regex = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))[0-9]{8}$";
        //创建一个条件（为了条件查询语句）
        QueryWrapper<Worker> wrapper = new QueryWrapper<>();
        //正则表达式查询结果
        boolean bool = keyword.matches(regex);
        //物业管理的表示，判断是否根据id查找
        PropertyTage propertyTage = propertyTageService.getById(1);
        //根据id查询
        if (keyword.length()>=4&&keyword.substring(0,4).equals(propertyTage.getTage())) {
            //sql语句(where workerId = keyword);
            wrapper.lambda().eq(Worker::getWorkerId,keyword);
            List<Worker> workers = workerService.list(wrapper);
            return workers;
        }else if (bool){
            //sql语句(where workerPhone = keyword);
            wrapper.lambda().eq(Worker::getWorkerPhone,keyword);
            List<Worker> workers = workerService.list(wrapper);
            return workers;
        }else {
            //sql语句(where workerName = keyword);
            wrapper.lambda().eq(Worker::getWorkerName,keyword);
            List<Worker> workers = workerService.list(wrapper);
            return workers;
        }

    }
}
